<template>
  <el-aside :width="isCollapse ? '64px': '200px'">
    <div class="toggle-button" @click="toggle">|||</div>
    <el-menu
      :default-active="defaultActive"
      router
      unique-opened
      :collapse="isCollapse"
      :collapse-transition="false"
      background-color="#324158"
      text-color="#fff"
      active-text-color="#ffd04b">
      <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
        <template slot="title">
          <i :class="iconObjs[item.id]"/>
          <span>{{ item.authName }}</span>
        </template>
        <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id"
                      @click="saveActive('/' + subItem.path)">
          <template slot="title">
            <i class="el-icon-menu"/>
            <span>{{ subItem.authName }}</span>
          </template>
        </el-menu-item>
      </el-submenu>
    </el-menu>
  </el-aside>
</template>

<script>
export default {
  name: 'AsideMenu',
  data () {
    return {
      menuList: [],
      iconObjs: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      defaultActive: window.sessionStorage.getItem('active')
    }
  },
  // 生命周期函数
  created () {
    this.getMenuList()
  },
  methods: {
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    toggle () {
      this.isCollapse = !this.isCollapse
    },
    saveActive (path) {
      window.sessionStorage.setItem('active', path)
      this.defaultActive = path
    }
  }
}
</script>

<style lang="less" scoped>
  .iconfont {
    margin-right: 10px;
  }
  .el-menu{
    border-right: none;
  }
  .toggle-button {
    background-color: #324158;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.3em;
    cursor: pointer;
  }
</style>
